package com.stevencheong.matrixSolver;

import Jama.Matrix;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Solver extends Activity {
	
	private EditText a00;
	private EditText a10;
	private EditText a20;
	private EditText a01;
	private EditText a11;
	private EditText a21;
	private EditText a02;
	private EditText a12;
	private EditText a22;
	
	private EditText b00;
	private EditText b01;
	private EditText b02;

	Matrix a_matrix;
	Matrix b_matrix;
	Matrix c_matrix;
	
	private Solver mContext;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        
        setContentView(R.layout.solver);
        
        // Initialize EditText's
        a00 = (EditText) findViewById(R.id.a00);
        a10 = (EditText) findViewById(R.id.a10);
        a20 = (EditText) findViewById(R.id.a20);
        a01 = (EditText) findViewById(R.id.a01);
        a11 = (EditText) findViewById(R.id.a11);
        a21 = (EditText) findViewById(R.id.a21);
        a02 = (EditText) findViewById(R.id.a02);
        a12 = (EditText) findViewById(R.id.a12);
        a22 = (EditText) findViewById(R.id.a22);
        
        b00 = (EditText) findViewById(R.id.b00);
        b01 = (EditText) findViewById(R.id.b01);
        b02 = (EditText) findViewById(R.id.b02);

        // Initialize Buttons
        Button solveButton = (Button) findViewById(R.id.btnSolve);
        
        // Add Listeners to Matrix Buttons
        solveButton.setOnClickListener(new OnClickListener() {
        	private AlertDialog show;
			public void onClick(View arg0) {
				
				try {
					putInputsIntoMatrices();
					
					c_matrix = a_matrix.solve(b_matrix);
					
//					c_matrix = Matrix.random(3,1);	//test
					
					show = new AlertDialog.Builder(mContext).setTitle("Result")
							.setMessage("x1=" + new Double( c_matrix.get(0, 0) ).toString() + "\n" +
										"x2=" + new Double( c_matrix.get(1, 0) ).toString() + "\n" +
										"x3=" + new Double( c_matrix.get(2, 0) ).toString() 
										)
							.setPositiveButton("OK", null).show(); 
				} catch (Exception e) {
					show = new AlertDialog.Builder(mContext).setTitle("Cannot Compute")
							.setMessage("Enter coefficients for system of linear equations ")
							.setPositiveButton("OK", null).show(); 
				}
			}
		});
    }
    
    //helper methods
	private void putAInputsIntoMatrixA() {
		double[][] a_arr = {
				{ new Double(a00.getText().toString()), new Double(a10.getText().toString()), new Double(a20.getText().toString()) },
				{ new Double(a01.getText().toString()), new Double(a11.getText().toString()), new Double(a21.getText().toString()) },
				{ new Double(a02.getText().toString()), new Double(a12.getText().toString()), new Double(a22.getText().toString()) },
		};
		
		a_matrix = new Matrix(a_arr);
	}
    
	private void putBInputsIntoMatrixB() {
		double[][] b_arr = {
				{ new Double(b00.getText().toString()) },
				{ new Double(b01.getText().toString()) },
				{ new Double(b02.getText().toString()) },
		};
		
		b_matrix = new Matrix(b_arr);
	}
	
	private void putInputsIntoMatrices() {
		putAInputsIntoMatrixA();
		putBInputsIntoMatrixB();
	}
}